'use strict';

var should = require('should');
var spider = require('../main');
var async = require('async');
var _ = require('lodash');
var debug = require('debug')('test_crawl');
var crypto = require('crypto');
var NewsController = require('../controller/NewsController');
var moment = require('moment');

describe('Test crawl',function(){
  it('crawl start',function(cb){
    async.parallel([
      function(done){
        /**
         * 开源项目
         * @type {String}
         */
        var feel_url = 'http://www.oschina.net/project/rss';
        spider.FetchRss(feel_url).then(function(data){
          async.mapSeries(data,function(i,callback){
            var md5 = crypto.createHash('md5');
            NewsController.create({
              title:i.title,
              link:i.link,
              date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
              author:i.author ? i.author : '开源中国',
              description:i.description,
              guid:md5.update(i.guid).digest('hex'),
              cate:'oschina',
              c_cate:'开源项目'
            },function(err){
              if(err) return done(err);
              callback(null,[]);
            });
          },function(err,res){
            done(null,'one');
          });
        });
      },
      function(done){
        /**
         * 开源新闻
         * @type {String}
         */
        var feel_url = 'http://www.oschina.net/news/rss';
        spider.FetchRss(feel_url).then(function(data){
          async.mapSeries(data,function(i,callback){
            var md5 = crypto.createHash('md5');
            NewsController.create({
              title:i.title,
              link:i.link,
              date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
              author:i.author ? i.author : '开源中国',
              description:i.description,
              guid:md5.update(i.guid).digest('hex'),
              cate:'oschina',
              c_cate:'开源新闻'
            },function(err){
              if(err) return done(err);
              callback(null,[]);
            });
          },function(err,res){
            done(null,'one');
          });
        });
      }
    ],function(error,result){
      // console.log(result);
      cb();
    });
  });
});